package com.e3.basdoc.warehouse.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.e3.basdoc.warehouse.model.WarehouseModel;
import com.e3.basdoc.warehouse.model.WarehouseStatus;
import com.e3.basdoc.warehouse.model.WarehouseType;
import com.e3.template.tgf.GFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.e3.web.widget.client.MyGridHelper;
import com.e3.web.widget.model.ListTypeModel;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class WarehouseHelper extends GFHelper<WarehouseModel>{
	
	public static WarehouseHelper INSTANCE = new WarehouseHelper();
	
	private String[] notNullFields = {WarehouseModel.CODE,WarehouseModel.NAME,WarehouseModel.TYPE};
	
	private Map<String,Integer> lengthMap = new HashMap<String, Integer>();
	
	private WarehouseHelper(){
		lengthMap.put(WarehouseModel.ID, 19);
		lengthMap.put(WarehouseModel.CODE,20);
		lengthMap.put(WarehouseModel.NAME,50);
		lengthMap.put(WarehouseModel.ADMINISTRATOR,20);
		lengthMap.put(WarehouseModel.CONTRACT_WAY,20);
	}

	public Field<?> createFormField(String name){
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		
		boolean isNotNull = isNotNullField(name);
		if(name.equals(WarehouseModel.ID)){
			formField = MyFormFieldFactory.createIdField(name);
		}		
		else if(name.equals(WarehouseModel.CODE)||name.equals(WarehouseModel.NAME)
			||name.equals(WarehouseModel.ADMINISTRATOR)
			||name.equals(WarehouseModel.CONTRACT_WAY)
			){
			int length = lengthMap.get(name);
			formField = MyFormFieldFactory.createStringField(name,dispName,length,isNotNull);
		}
		else if(name.equals(WarehouseModel.TYPE)){
			formField = MyFormFieldFactory.createListField(name, dispName, getTypeListTypeModel(),true,isNotNull);
		}
		else if(name.equals(WarehouseModel.STATUS)){
			formField = MyFormFieldFactory.createListField(name, dispName, getStatusListTypeModel(),true,isNotNull);
		}
		return formField;
	}
	
	private List<ListTypeModel<Integer>> getTypeListTypeModel(){
		List<ListTypeModel<Integer>> typeValues = new ArrayList<ListTypeModel<Integer>>();
		Integer code = WarehouseType.TYPE_NORMAL;
		ListTypeModel<Integer> ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(WarehouseModel.TYPE_NORMAL_NAME));
		typeValues.add(ltModel);
		code = WarehouseType.TYPE_RETURN;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(WarehouseModel.TYPE_RETURN_NAME));
		typeValues.add(ltModel);
		code = WarehouseType.TYPE_REJECTION;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(WarehouseModel.TYPE_REJECTION_NAME));
		typeValues.add(ltModel);
		return typeValues;
	}
	
	private List<ListTypeModel<Integer>> getStatusListTypeModel(){
		List<ListTypeModel<Integer>> statusValues = new ArrayList<ListTypeModel<Integer>>();
		Integer code = WarehouseStatus.STATUS_NORMAL;
		ListTypeModel<Integer> ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(WarehouseModel.STATUS_NORMAL_NAME));
		statusValues.add(ltModel);
		code = WarehouseStatus.STATUS_SEAL;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(WarehouseModel.STATUS_SEAL_NAME));
		statusValues.add(ltModel);
		return statusValues;
	}
	
	public List<ColumnConfig> getGridColumnConfigs(){
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();   
		configs.add(numberer);
		
		ColumnConfig column = new ColumnConfig();
		String name = WarehouseModel.CODE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
	    
	    column = new ColumnConfig();
		name = WarehouseModel.NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
	    
	    column = new ColumnConfig();
		name = WarehouseModel.ADMINISTRATOR;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = WarehouseModel.CONTRACT_WAY;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
		    
	    Object[] typeCodes = {WarehouseType.TYPE_NORMAL,WarehouseType.TYPE_RETURN,WarehouseType.TYPE_REJECTION};
		String s1 = getFieldDisplayName(WarehouseModel.TYPE_NORMAL_NAME);
		String s2 = getFieldDisplayName(WarehouseModel.TYPE_RETURN_NAME);
		String s3 = getFieldDisplayName(WarehouseModel.TYPE_REJECTION_NAME);
		String[] typeNames = {s1,s2,s3};
	    GridCellRenderer<WarehouseModel> typeCellRenderer = MyGridHelper.createListCellRender(WarehouseModel.TYPE, typeCodes, typeNames);
		column = new ColumnConfig();
		name = WarehouseModel.CONTRACT_WAY;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);  
	    column.setRenderer(typeCellRenderer);
	    configs.add(column);
	    
	    Object[] statusCodes = {WarehouseStatus.STATUS_NORMAL,WarehouseStatus.STATUS_SEAL};
	    String st1 = getFieldDisplayName(WarehouseModel.STATUS_NORMAL_NAME);
	    String st2 = getFieldDisplayName(WarehouseModel.STATUS_SEAL_NAME);
	    String[] statusNames = {st1,st2};
	    GridCellRenderer<WarehouseModel> statusCellRenderer = MyGridHelper.createListCellRender(WarehouseModel.STATUS, statusCodes, statusNames);
		column = new ColumnConfig();
		name = WarehouseModel.STATUS;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200); 
	    column.setRenderer(statusCellRenderer);
	    configs.add(column);
	    
	    return configs;
	}

	
	public String getFieldDisplayName(String code) {
		return WarehouseFieldsConstant.INSTANCE.getString(code);
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFields;
	}

	@Override
	public WarehouseModel createModelInstance() {
		WarehouseModel model = new WarehouseModel();
		model.setStatus(WarehouseStatus.STATUS_NORMAL);
		model.setType(WarehouseType.TYPE_NORMAL);
		return model;
	}

}
